package day02;

/**
 * @author Jetwang
 * @version 1.0
 * @since 2023/3/2 20:14
 * 力扣 83
 */
public class LinkedListPractice04 {

    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode p1 = head;
        ListNode p2 = null;
        while((p2 = p1.next) != null) {
            if (p1.val == p2.val) {
                // 删除p2
                p1.next = p2.next;
            } else {
                // 向后平移
                p1 = p1.next;
            }
        }
        return head;
    }

    public ListNode deleteDuplicates02(ListNode head) {
        if (head == null || head.next == null) return head;
        if (head.val == head.next.val) {
            return deleteDuplicates02(head.next);
        } else {
            head.next = deleteDuplicates02(head.next);
            return head;
        }
    }
}
